package com.company.oa.workflow.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.oa.common.api.Result;
import com.company.oa.workflow.dto.expense.ExpenseDTO;
import com.company.oa.workflow.entity.expense.Expense;
import com.company.oa.workflow.service.ExpenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/workflow/expense")
public class ExpenseController {
    
    @Autowired
    private ExpenseService expenseService;
    
    /**
     * 分页查询报销列表
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('workflow:expense:list')")
    public Result<IPage<Expense>> getPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            Expense expense) {
        IPage<Expense> pageResult = expenseService.getPage(page, size, expense);
        return Result.success(pageResult);
    }
    
    /**
     * 获取报销详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('workflow:expense:info')")
    public Result<Map<String, Object>> getExpenseDetail(@PathVariable Long id) {
        Map<String, Object> detail = expenseService.getExpenseDetail(id);
        return Result.success(detail);
    }
    
    /**
     * 创建报销申请（草稿）
     */
    @PostMapping
    @PreAuthorize("hasAuthority('workflow:expense:create')")
    public Result<Boolean> createExpense(@RequestBody @Valid ExpenseDTO expenseDTO) {
        // 从当前登录用户中获取用户ID
        Long userId = 1L; // 实际项目中应从SecurityContext中获取
        boolean success = expenseService.createExpense(expenseDTO, userId);
        return Result.success(success);
    }
    
    /**
     * 更新报销申请
     */
    @PutMapping
    @PreAuthorize("hasAuthority('workflow:expense:update')")
    public Result<Boolean> updateExpense(@RequestBody @Valid ExpenseDTO expenseDTO) {
        boolean success = expenseService.updateExpense(expenseDTO);
        return Result.success(success);
    }
    
    /**
     * 提交报销申请
     */
    @PostMapping("/submit/{id}")
    @PreAuthorize("hasAuthority('workflow:expense:submit')")
    public Result<Boolean> submitExpense(@PathVariable Long id) {
        boolean success = expenseService.submitExpense(id);
        return Result.success(success);
    }
    
    /**
     * 取消报销申请
     */
    @PostMapping("/cancel/{id}")
    @PreAuthorize("hasAuthority('workflow:expense:cancel')")
    public Result<Boolean> cancelExpense(@PathVariable Long id) {
        boolean success = expenseService.cancelExpense(id);
        return Result.success(success);
    }
    
    /**
     * 上传附件
     */
    @PostMapping("/upload")
    @PreAuthorize("hasAuthority('workflow:expense:upload')")
    public Result<String> uploadAttachment(@RequestParam("file") MultipartFile file) {
        String filePath = expenseService.uploadAttachment(file);
        return Result.success(filePath);
    }
    
    /**
     * 获取我的报销列表
     */
    @GetMapping("/my")
    public Result<IPage<Expense>> getMyExpenses(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Integer status) {
        // 从当前登录用户中获取用户ID
        Long userId = 1L; // 实际项目中应从SecurityContext中获取
        IPage<Expense> pageResult = expenseService.getMyExpenses(page, size, userId, status);
        return Result.success(pageResult);
    }
    
    /**
     * 支付报销
     */
    @PostMapping("/pay/{id}")
    @PreAuthorize("hasAuthority('workflow:expense:pay')")
    public Result<Boolean> payExpense(@PathVariable Long id) {
        // 从当前登录用户中获取用户ID
        Long userId = 1L; // 实际项目中应从SecurityContext中获取
        boolean success = expenseService.payExpense(id, userId);
        return Result.success(success);
    }
} 